package org.jinyan.wechat.controller;

import java.lang.reflect.Type;
import java.sql.SQLException;
import java.util.Map;

import org.jinyan.db.constant.Tables;
import org.jinyan.db.dao.ISuperMapDao;
import org.jinyan.db.dao.impl.SuperMapDao;
import org.jinyan.db.utils.BeanMapConverUtils;
import org.jinyan.wechat.model.WechatMessage;

import com.google.gson.reflect.TypeToken;

public class WechatMessageController {

	private ISuperMapDao superMapDao = new SuperMapDao();

	public WechatMessage addMessage(WechatMessage message) throws SQLException {
		Map messMap = BeanMapConverUtils.beanToMap(message);
		superMapDao.add(Tables.wechat_message_tb, messMap);
		return message;
	}

	public WechatMessage deleteMessage(WechatMessage wechatMess) throws SQLException {
		Map messMap = BeanMapConverUtils.beanToMap(wechatMess);
		superMapDao.delete(Tables.wechat_message_tb, messMap);
		return wechatMess;
	}

	public WechatMessage updateMessage(WechatMessage wechatMess, String pk)
			throws SQLException {
		Map messMap = BeanMapConverUtils.beanToMap(wechatMess);
		superMapDao.update(Tables.wechat_message_tb, messMap, pk);
		return wechatMess;
	}

	public WechatMessage selectMessage(WechatMessage wechatMess) throws SQLException {
		Map messMap = BeanMapConverUtils.beanToMap(wechatMess);
		Map result = (Map) superMapDao
				.select(Tables.wechat_message_tb, messMap);
		Type type = new TypeToken<WechatMessage>() {
		}.getType();
		wechatMess = (WechatMessage) BeanMapConverUtils.mapToBean(result, type);
		return wechatMess;
	}

	public WechatMessage selectMessage(Map messMap) throws SQLException {
		Map result = (Map) superMapDao
				.select(Tables.wechat_message_tb, messMap);
		Type type = new TypeToken<WechatMessage>() {
		}.getType();
		WechatMessage wechatMess = (WechatMessage) BeanMapConverUtils
				.mapToBean(result, type);
		return wechatMess;
	}

}
